import { reportEventData } from '@/dataReport/dataReportUtil'
import keyUtils from '@/dataReport/keyUtils'
import aesHelper from '@/utils/aesHelper'
import store from '@/store'
import { useAuthStore } from '@/store/auth'

const app = getApp()

let authStore = useAuthStore(store)

let logger = wx.getLogManager({
  level: 1,
})

/**
 * 数据上报
 * @param {*} eventName
 * @param {*} propObj
 */
function pluginReportEventData(eventName, propObj, that) {
  reportEventData(eventName, propObj, that)
}

/**
 * 插件日志接口
 * @param {日志标签} tag
 * @param  {...any} args
 */
function pluginConsole(tag, ...args) {
  console.log('pluginConsole', tag, args)
  if (args) {
    if (args.length < 1) {
      console.error('pluginConsole', 'args is null')
    } else if (args.length < 2) {
      logger.log(tag, args[0])
    } else if (args.length < 3) {
      logger.log(tag, args[0], args[1])
    } else if (args.length < 4) {
      logger.log(tag, args[0], args[1], args[2])
    }
  }
}

/**
 * 插件初始化语言环境
 */
function initLanguage() {
  return wx.getStorageSync('Language')
}

/**
 * 保存插件token
 * @param {*} token
 */
function saveToken(token, ciamId) {
  authStore.loginRefresh = true
  if (ciamId) {
    authStore.accessPhoneNumber = ciamId.replace(/^(\+{0,1}86){0,1}/, '')
    wx.setStorageSync('ciamId', aesHelper.encrypt(authStore.accessPhoneNumber, authStore.key, authStore.iv))
  }
  authStore.updateToken(token)
  wx.hideLoading()
}
module.exports = {
  pluginReportEventData,
  pluginConsole,
  initLanguage,
  saveToken,
}
